package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemMenuButtonDao;
import com.example.child_protection.dto.info.SystemMenuButtonDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.CheckCodeRequestDTO;
import com.example.child_protection.dto.request.find.CkeckUserNameRequestDTO;
import com.example.child_protection.dto.request.find.SystemMenuButtonRequestDTO;
import com.example.child_protection.dto.response.CheckCodeResponseDTO;
import com.example.child_protection.dto.response.CheckUserResponseDTO;
import com.example.child_protection.dto.response.SystemMenuButtonListResponseDTO;
import com.example.child_protection.dto.response.SystemMenuButtonResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemMenuButtonModel;
import com.example.child_protection.model.UserModel;
import com.example.child_protection.service.SystemMenuButtonQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 菜单按钮查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemMenuButtonQueryServiceImpl extends MyBaseService implements SystemMenuButtonQueryService {

    @Autowired
    public SystemMenuButtonDao systemMenuButtonDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemMenuButtonResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_MENU_BUTTON_FIND_ID, requestDTO, SystemMenuButtonResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemMenuButtonResponseDTO>() {
            @Override
            protected SystemMenuButtonResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemMenuButtonResponseDTO responseDTO = new SystemMenuButtonResponseDTO();
                SystemMenuButtonModel systemMenuButtonModel = systemMenuButtonDao.findId(requestDTO.getId());
                SystemMenuButtonDTO systemMenuButton = new SystemMenuButtonDTO();
                BeanUtils.copyProperties(systemMenuButtonModel, systemMenuButton);
                responseDTO.setInfo(systemMenuButton);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemMenuButtonListResponseDTO findList(SystemMenuButtonRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_MENU_BUTTON_FIND_LIST, requestDTO, SystemMenuButtonListResponseDTO.class, new MyServiceExecutor<SystemMenuButtonRequestDTO, SystemMenuButtonListResponseDTO>() {
            @Override
            protected SystemMenuButtonListResponseDTO myprocess(SystemMenuButtonRequestDTO requestDTO) throws Exception {
                SystemMenuButtonListResponseDTO responseDTO = new SystemMenuButtonListResponseDTO();
                SystemMenuButtonModel req = new SystemMenuButtonModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemMenuButtonModel> lists = systemMenuButtonDao.findPage(req);
                List<SystemMenuButtonDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemMenuButtonDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 检测CODE是否重复
     * @param requestDTO
     * @return
     */
    public CheckCodeResponseDTO checkCode(CheckCodeRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_MENU_BUTTON_CHECK_CODE, requestDTO, CheckCodeResponseDTO.class, new MyServiceExecutor<CheckCodeRequestDTO, CheckCodeResponseDTO>() {
            @Override
            protected CheckCodeResponseDTO myprocess(CheckCodeRequestDTO requestDTO) throws Exception {
                CheckCodeResponseDTO res = new CheckCodeResponseDTO();
                SystemMenuButtonModel req = new SystemMenuButtonModel();
                req.setCode(requestDTO.getCode());
                int result = systemMenuButtonDao.count(req);
                if (result > 0) {
                    res.setFlag(BooleanEnums.T);
                } else {
                    res.setFlag(BooleanEnums.F);
                }
                res.successfully();
                return res;
            }
        });
    }
}
